<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.nickname" placeholder="昵称" clearable style="width: 250px;" class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.isDeleted" placeholder="状态" clearable style="width: 110px" class="filter-item">
        <el-option label="正常" :value="0" />
        <el-option label="已删除" :value="1" />
      </el-select>
      &nbsp;
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="#" fixed="left" width="40" align="center" type="index"
        :index="(i)=>{ return (listQuery.pageNum - 1) * listQuery.pageSize + i + 1 }">
      </el-table-column>
      <el-table-column label="昵称" fixed="left" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.nickname }} </span>
        </template>
      </el-table-column>
      <el-table-column label="组织" width="150px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.sysGroup">{{ row.sysGroup.groupName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色" width="150px" align="center">
        <template slot-scope="{row}">
          <el-tag size="mini" type="danger" v-if="row.isAdministrator">系统管理员</el-tag>
          <el-tag size="mini" v-for="item in row.sysRoleList" style="margin-right: 3px;">
            {{ item.roleName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column label="推送配置" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ handleMessageTypes(row.messageTypes) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gmtCreate | dateFormat() }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gmtModified | dateFormat() }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.isDeleted | isDeletedFilter">
            {{ row.isDeleted ? '已删除' : '正常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import {
    SYS_USER_API
  } from '@/api/systemApi.js'

  const messageTypesOptions = [{
    "label": "站内信",
    "value": "MESSAGE"
  }, {
    "label": "邮箱",
    "value": "EMAIL"
  }, {
    "label": "短信",
    "value": "SMS"
  }, {
    "label": "微信模板消息",
    "value": "WECHAT_TEMPLATE"
  }];

  export default {
    components: {
      Pagination
    },
    data() {
      return {
        listLoading: true,
        list: null,
        total: 0,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          isDeleted: undefined,
          nickname: undefined
        }
      }
    },
    filters: {
      isDeletedFilter(isDeleted) {
        return isDeleted ? 'danger' : 'success'
      }
    },
    created() {
      this.handleFilter()
    },
    methods: {
      async getList() {
        this.listLoading = true
        var res = await SYS_USER_API.listUsers(this.listQuery);
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      },
      handleIndex(index) {
        return (this.listQuery.pageNum - 1) * this.listQuery.pageSize + index + 1
      },
      handleFilter() {
        this.listQuery.pageNum = 1
        this.getList()
      },
      handleMessageTypes(messageTypes) {
        if (!messageTypes) return '';
        return messageTypesOptions.filter(x => messageTypes.indexOf(x.value) !== -1).map(x => x.label);
      },

    }
  }
</script>
